#include "MapUnit.h"

CMapUnit::CMapUnit(hgeSprite* s, int type, int row, int col)
:m_MapUnit(s), m_MapType(type)
{
	float w = s->GetWidth();
	float h = s->GetHeight();
	SetRect(w*col,h*row,w,h);

	if(m_MapType == MAP_GRASS)
		m_IsBlock = false;
	else
		m_IsBlock = true;

	switch(m_MapType)
	{
	case MAP_WALL:
	case MAP_STEEL:
	case MAP_HOME_W:
		SetCanDestory(true);
		break;
	default:
		SetCanDestory(false);
		break;
	}
	m_Type = TYPE_UNIT;
}

CMapUnit::~CMapUnit(void)
{
}

void CMapUnit::DoRender()
{
	if(m_MapUnit)
		m_MapUnit->Render(GetX(),GetY());
}


CMapUnitFactory* CMapUnitFactory::m_MapUnitFactory = NULL;

CMapUnitFactory::CMapUnitFactory(void)
{
}

CMapUnitFactory::~CMapUnitFactory(void)
{
	if(!m_MapUnitList.empty())
		m_MapUnitList.clear();
}

CMapUnitFactory* CMapUnitFactory::GetFactory()
{
	if(m_MapUnitFactory == NULL)
		m_MapUnitFactory = new CMapUnitFactory();
	return m_MapUnitFactory;
}

void CMapUnitFactory::CloseFactory()
{
	if(m_MapUnitFactory)
		delete m_MapUnitFactory;
	m_MapUnitFactory = NULL;
}

CMapUnit* CMapUnitFactory::CreateMapUnit(int type, int row, int col)
{
	CMapUnit *unit = NULL;
	if(type > 0 && type < MAP_UNIT_COUNT+1)
	{
		type--;
		unit = new CMapUnit(CResManager::GetInstance()->m_spriteMapUnit[type],type,row,col);
		m_MapUnitList.push_back(unit);
	}
	return unit;
}